import utils from '@/libs/utils'
import serverUtils from './utils'
import api from './request'
const {
    getSongListCategories,
    getSongLists,
    getSongListDetails,
    getLyricByIds,
    findMusicByName,
    carouselRand,
    songlistRand,
    musicRand
} = api;
export default class Service {
    async getSongListTags() {
        let data = utils.getSessionStorage("SongListTags");
        if (data) {
            return JSON.parse(data);
        } else {
            const result = await getSongListCategories();
            data = result.data.data.value;
            let newArr = [];
            for (let key in data.categories) {
                newArr[key] = {
                    name: data.categories[key],
                    subs: []
                }
            }
            data.sub.map((item) => {
                newArr[item.category].subs.push(item);
            })

            utils.setSessionStorage("SongListTags", newArr);
            return newArr;
        }
    }

    async getSongList(params) {
        const result = await getSongLists(params);
        let data = result.data.data.value;

        return data;
    }

    async getSongListDetail(params) {
        let data = utils.getSessionStorage("SongListDetail");
        if (data) {
            data = JSON.parse(data);
            if (data.list_id == params.list_id) {
                return data;
            }
        }
        const result = await getSongListDetails(params);
        data = result.data.data.value;
        utils.setSessionStorage("SongListDetail", data);
        return data;
    }


    async getLyricById(params) {
        const result = await getLyricByIds(params);
        let data = result.data.data.value[0];
        if (data.lyric) {
            let sArr = serverUtils.lyricArr(data.lyric); // 把字符串歌词转成数组
            let nArr = [];
            if (data.tlyric.lyric) { //判断是否有翻译歌词
                sArr = serverUtils.tlyricArr(sArr, data.tlyric.lyric);
            }
            for (let item in sArr) {
                nArr.push(sArr[item]);
            }
            return nArr;
        }
    }

    async findMusicByNames(params) {
        const result = await findMusicByName(params);
        let data = result.data.data.value
        return data;
    }

    async findcarouselRand() {
        const result = await carouselRand();
        let data = result.data.data.value
        return data;
    }
    async findsonglistRand() {
        const result = await songlistRand();
        let data = result.data.data.value
        return data;
    }

    async findmusicRand(params) {
        const result = await musicRand(params);
        let data = result.data.data.value
        return data;
    }


    // async getMusicPlay(params) {
    //     const value = await this.GET_GetMusicPlays(params);
    //     return value.data.data;
    // }
}